//--------------------Sonic CD HUD Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
// Not all of these are used here in the HUD script, some are just used as a way for other objects to globally transmit values


// Game Mode Aliases
private alias 2 : MODE_TIMEATTACK

// Priority
private alias 1 : PRIORITY_ACTIVE

public value UseFlashlight = 0;
public value MouseEnabled = 0;
public value FlashBattery = 5000;
public value HUDEnabled = 0;
public value CutsceneActive = 0;
public value OpenBook = 0;

private alias Object.Value0 : Object.PageCount 

private alias object.value2 : bg.scroll

public alias saveRAM[295]: saved.mobile
public alias saveRAM[296]: saved.transmobile


private alias object.value11 : touchoffset
private alias object.value12 : pauseoffset

private alias object.value33 : touch.fade
private alias object.value34 : buttonoffset

reserve function MainHUD
reserve function TouchControls
reserve function CraftingUI

function CraftingUI
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 192)

		//THE BOOKS
		switch object.state
		case 0
			temp3 = screen.xcenter
			DrawSpriteScreenXY(36, temp3, screen.ycenter)//the board


		break
		case 1
			temp3 = screen.xcenter
			temp3 += 72
			DrawSpriteScreenXY(36, temp3, screen.ycenter)//the board
			DrawSpriteScreenXY(39, temp3, screen.ycenter)//the reciepe book open

			CheckTouchRect(52, 72, 82, 96)
			if checkResult > -1
				OpenBook = 2
				PlaySfx(SfxName[Score Add], 0)				
			end if

		break
		end switch
	



		//NUMBERS
		if OpenBook < 2
			temp1 = screen.xcenter
			if OpenBook >= 1
				temp1 += 16
			else
				temp1 -= 56
			end if
			temp2 = screen.ycenter
			temp2 += 69
			DrawSpriteScreenXY(40, temp1, temp2)
		end if

		



		if OpenBook == 1
			CheckTouchRect(52, 72, 82, 96)
			if checkResult > -1
				OpenBook = 2
				PlaySfx(SfxName[Score Add], 0)				
			end if
		end if

end function

function MainHUD
		//FLASHLIGHT
		UseFlashlight = 1
		if ThunderActive == 0
			if UseFlashlight == 1
				if FlashBattery > 0	//battery greater then 0?
					if object[2].controlmode != -1	//can the player move?
						if inputdown[2].buttonY == 0	//if your holding the button
							DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 192)
						else
						
							if inputpress[2].buttonY == 1
								PlaySfx(SfxName[Flashlight], 0)
							end if

							FlashBattery -= 1

							temp0=128
							temp0*=Object.Scale
							temp0>>=9
							

							temp5=256
							temp5*=Object.Scale
							temp5>>=9

							temp1=213
							temp1-=temp0

							temp2=temp1
							temp2+=temp5

							temp3=120
							temp3-=temp0

							temp4=temp3
							temp4+=temp5

							DrawRect(0, 0, temp1, screen.ysize, 0x00, 0x00, 0x00, 192)
							DrawRect(temp2, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 192)
							DrawRect(temp1, 0, temp5, temp3, 0x00, 0x00, 0x00, 192)
							DrawRect(temp1, temp4, temp5, screen.ysize, 0x00, 0x00, 0x00, 192)
							
							temp6 = screen.xsize
							temp6 -= screen.xcenter
							
							DrawSpriteScreenFX(15,FX_INK,temp6,screen.ycenter)
							object.InkEffect = INK_ALPHA
							object.alpha = 200
						end if		
					else
						DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 192)	
					end if	
				else	//out of power ur cooked
					DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 224)

					if inputpress[2].buttonY == 1
						PlaySfx(SfxName[Error], 0)
					end if
				end if		
			end if		
		else
			DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 96)	
		end if	


		//BATTERY
		temp3 = screen.xcenter
		DrawSpriteScreenXY(17, temp3, screen.ycenter)

		//
		if FlashBattery > 4000
			DrawSpriteScreenXY(18, screen.xcenter, screen.ycenter)
		end if

		if FlashBattery > 3000
			DrawSpriteScreenXY(19, screen.xcenter, screen.ycenter)
		end if	

		if FlashBattery > 2000
			DrawSpriteScreenXY(20, screen.xcenter, screen.ycenter)
		end if		

		if FlashBattery > 1000
			DrawSpriteScreenXY(21, screen.xcenter, screen.ycenter)
		end if	

		if FlashBattery > 0
			DrawSpriteScreenXY(22, screen.xcenter, screen.ycenter)
		end if	

		//HEALTH

		temp1 = screen.xcenter
		temp1 -= 0x90
		temp2 = screen.ycenter
		temp2 -= 0x50

		DrawSpriteScreenXY(35, temp1, temp2)

		if PlayerHealth >= 3
			DrawSpriteScreenXY(34, temp1, temp2)
		end if

		temp1 += 0x18

		DrawSpriteScreenXY(35, temp1, temp2)

		if PlayerHealth >= 2
			DrawSpriteScreenXY(34, temp1, temp2)
		end if

		temp1 += 0x18

		DrawSpriteScreenXY(35, temp1, temp2)

		if PlayerHealth >= 1
			DrawSpriteScreenXY(34, temp1, temp2)
		end if



		// Find the initial X Position to draw the HUD elements
		temp1 = Screen.xcenter
		temp1 -= 108
		
		// Draw the "UFO" graphic
		DrawSpriteScreenXY(10, temp1, 20)
		
		// Draw the first digit of the UFOs counter
		temp0 = Object.PageCount
		temp0 /= 10
		temp1 += 24
		DrawSpriteScreenXY(temp0, temp1, 21)
		
		// Draw the second digit of the UFOs counter
		temp0 = Object.PageCount
		temp0 %= 10
		temp1 += 8
		DrawSpriteScreenXY(temp0, temp1, 21)
end function

function TouchControls
	object.inkEffect = INK_ALPHA

	temp0 = screen.xsize
	temp0 -= 44
	DrawSpriteScreenXY(49,  26,1 49) //dpad
	temp0 -= 39

	DrawSpriteScreenXY(56, 0, temp0, 8) //pause

	//movement
	
	temp0 -= 10
	if inputDown.Left==1
		DrawSpriteScreenXY(52,  26, 151)
	endif
	if inputDown.Down==1
		DrawSpriteScreenXY(51,  26, 189)
	end if
	if inputDown.Right==1
		DrawSpriteScreenXY(53,  64, 151)
	endif
	if inputDown.Up==1
		DrawSpriteScreenXY(50,  26, 151)
	endif
	
	temp0 += 15	
	//jump
	if inputDown.buttonA==1
		DrawSpriteScreenXY(55,  temp0, 163)	
	else
		DrawSpriteScreenXY(54,  temp0, 163)
	endif

	temp0 -= 50
	//flashlight
	if inputDown.buttonY==1
		DrawSpriteScreenXY(58,  temp0, 54)	
	else
		DrawSpriteScreenXY(57,  temp0, 54)
	endif	

	temp0 += 80
	DrawSpriteScreenXY(59,  temp0, 6)
end function



event ObjectDraw
//Not Dead
if HUDEnabled == 1
	if object[2].state != 13
		CallFunction(MainHUD)
	else	
		//Dead
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 224)
	end if
else
	if CutsceneActive == 1
		CallFunction(CraftingUI)
	else
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 192)
	end if
end if

	//jumpscare


	//touch controls
	if saved.mobile == 1
		CallFunction(TouchControls)
		DrawSpriteScreenXY(16, touchscreen[0].xpos, touchscreen[0].ypos)
	end if


	if MouseEnabled == 1
		DrawSpriteScreenXY(16, touchscreen[0].xpos, touchscreen[0].ypos)
	end if



end event


event ObjectStartup
	
	LoadSpriteSheet("3D_Test/Objects.gif")
	
	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[4].Type = TypeName[HUD JR]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[4].Priority = PRIORITY_ACTIVE
	
	// Make the HUD draw ontop of all other sprites, too
	Object[4].DrawOrder = 5
	
	HUDEnabled = 1



	// 0-9 - Numbers 0-9
	SpriteFrame(0, 0, 8, 13, 206, 84)
	SpriteFrame(0, 0, 8, 13, 215, 84)
	SpriteFrame(0, 0, 8, 13, 224, 84)
	SpriteFrame(0, 0, 8, 13, 233, 84)
	SpriteFrame(0, 0, 8, 13, 242, 84)
	SpriteFrame(0, 0, 8, 13, 206, 98)
	SpriteFrame(0, 0, 8, 13, 215, 98)
	SpriteFrame(0, 0, 8, 13, 224, 98)
	SpriteFrame(0, 0, 8, 13, 233, 98)
	SpriteFrame(0, 0, 8, 13, 242, 98)
	
	// 10 - UFO Counter Graphic
	SpriteFrame(0, 0, 24, 16, 206, 58)
	
	// 11 - Time Graphic
	SpriteFrame(0, 0, 24, 8, 231, 58)
	
	// 12 - Rings Graphic
	SpriteFrame(0, 0, 31, 8, 206, 75)
	
	// 13 - Single Tick Graphic
	SpriteFrame(0, 0, 8, 13, 224, 112)
	
	// 14 - Double Tick Graphic
	SpriteFrame(0, 0, 8, 13, 233, 112)


	//15
	SpriteFrame(-128,-128,256,256,256,256)	


	// 16 - Double Tick Graphic
	SpriteFrame(0, 0, 12, 21, 245, 1)


	// 17 - battery bar
	SpriteFrame(-200, -32, 64, 64, 257, 116)


	// 18 - battery bar
	SpriteFrame(-194, -22, 24, 8, 322, 116)
	SpriteFrame(-194, -12, 24, 8, 322, 126)//19
	SpriteFrame(-194, -2, 24, 8, 322, 136)	//20
	SpriteFrame(-194,  8, 24, 8, 322, 146)	//21
	SpriteFrame(-194,  18, 24, 8, 322, 156)	//22



	// 23 - SLENDERMAN!!
	SpriteFrame(-30, -24, 63, 112, 339, 1)


	SpriteFrame(-34, -30, 68, 30, 0, 412) //107 Static
	SpriteFrame(-34, -30, 68, 30, 0, 442)//25
	SpriteFrame(-34, -30, 68, 30, 0, 472)//26
	SpriteFrame(-34, -30, 68, 30, 68, 412)//27
	SpriteFrame(-34, -30, 68, 30, 68, 442)//28
	SpriteFrame(-34, -30, 68, 30, 68, 472)//29
	SpriteFrame(-34, -30, 68, 30, 136, 412)//30
	SpriteFrame(-34, -30, 68, 30, 136, 442)//31
	SpriteFrame(-34, -30, 68, 30, 136, 472)//32


	SpriteFrame(-24, -30, 64, 64, 0, 413) //33


	// lazy filler
	SpriteFrame(-48, -24, 20, 24, 52, 59) //34 ring
	SpriteFrame(-48, -23, 20, 22, 83, 180) //35 ring
	SpriteFrame(-82, -82, 176, 166, 1, 513)	//36	Crafting UI
	SpriteFrame(0, 0, 20, 18, 178, 580)	//37	buton
	SpriteFrame(0, 0, 0, 0, 0, 0)	//38
	SpriteFrame(-240, -82, 147, 166, 199, 513)	//39	Reciepe UI
	SpriteFrame(0, 0, 18, 7, 178, 615)	//40 12x
	SpriteFrame(0, 0, 12, 21, 245, 1)	//41
	SpriteFrame(0, 0, 12, 21, 245, 1)	//42
	SpriteFrame(0, 0, 12, 21, 245, 1)	//43
	SpriteFrame(0, 0, 12, 21, 245, 1)	//44
	SpriteFrame(0, 0, 12, 21, 245, 1)	//45
	SpriteFrame(0, 0, 12, 21, 245, 1)	//46
	SpriteFrame(0, 0, 12, 21, 245, 1)	//47
	SpriteFrame(0, 0, 12, 21, 245, 1)	//48	

	SpriteFrame(0,0,64,66,126,257) //49 DPAD
	SpriteFrame(26,0,12,25,242,321) //50 DPAD up press
	SpriteFrame(26,0,12,26,242,359) //51 DPAD down press
	SpriteFrame(0,25,26,13,215,358) //52 DPAD left press
	SpriteFrame(0,25,26,13,215,372) //53 DPAD right press
	SpriteFrame(0,0,48,48,191,257) //54 A button
	SpriteFrame(0,0,48,48,191,306) //55 A button down

	SpriteFrame(32,0,16,16,174,325) // 56 Pause Button

	SpriteFrame(0,0,33,33,123,325) // 57 Super Button - Mobile

	SpriteFrame(0,0,33,33,89,325) // 58 Super Button - Mobile

	SpriteFrame(0,0,16,16,174,325) // 59 Pause Button again
	

end event


// ========================
// Editor events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(0, 0, 24, 16, 206, 58)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
